package com.miniot.base.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.miniot.base.entity.WarehouseManage;
import com.miniot.base.service.WarehouseManageService;
import com.miniot.base.vo.WarehouseManageQueryVO;
import com.miniot.base.vo.WarehouseManageVO;
import com.miniot.business.base.BaseController;
import com.miniot.business.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 仓库表
 *
 * @author zzq
 * @date 2023-10-17 11:15:34
 */
@RestController
@AllArgsConstructor
@RequestMapping("/warehousemanage" )
@Api(value = "仓库表(后台)", tags = "仓库表-后台接口")

public class WarehouseManageController extends BaseController {

    private final WarehouseManageService warehouseManageService;

    /**
    * 分页查询
    * @param currentPage 当前页数
    * @param pageSize 每页数据量
    * @param query 查询条件
    * @return 查询结果
    */
    @GetMapping("/listPage")
    @ApiOperation(value = "分页查询", notes = "传入分页信息与查询条件")
    public R<Page<WarehouseManage>> listWarehouseManagePage(
            @ApiParam(value = "当前页数", required = true, defaultValue = "1") @RequestParam Long currentPage,
            @ApiParam(value = "每页数据量", required = true, defaultValue = "10") @RequestParam Long pageSize,
            @ApiParam(value = "查询条件") WarehouseManageQueryVO query) {
        Page<WarehouseManage> page = new Page<WarehouseManage>(currentPage, pageSize);
        return R.success(warehouseManageService.page(page, new LambdaQueryWrapper<WarehouseManage>()
                .like(ObjectUtil.isNotEmpty(query.getWarehouseName()),WarehouseManage::getWarehouseName,query.getWarehouseName())
                .orderByDesc(WarehouseManage::getCreateTime)),"分页信息查询成功");
    }


    /**
    * 通过id查询仓库表
    * @param id id
    * @return Result
    */
    @GetMapping("/getById")
    @ApiOperation(value = "通过id查询仓库表", notes = "传入id")
    public R<WarehouseManage> getWarehouseManageById(@ApiParam(value = "id", required = true) @RequestParam("id") Long id) {
        return R.success(warehouseManageService.getById(id));
    }

    /**
    * 新增仓库表
    * @param warehouseManage 仓库表
    * @return Result
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增仓库表", notes = "传入WarehouseManage数据")
    public R<Boolean> save(@RequestBody @Validated WarehouseManageVO warehouseManage) {
        WarehouseManage warehouseManage1 = new WarehouseManage();
        BeanUtil.copyProperties(warehouseManage,warehouseManage1);
        Assert.isTrue(warehouseManageService.save(warehouseManage1), "操作失败");
        return R.success(true,"新增成功");
    }

    /**
    * 修改仓库表
    * @param warehouseManage 仓库表
    * @return Result
    */
    @PostMapping("/updateById")
    @ApiOperation(value = "修改仓库表", notes = "传入WarehouseManage数据")
    public R<Boolean> updateById(@RequestBody WarehouseManageVO warehouseManage) {
        WarehouseManage warehouseManage1 = new WarehouseManage();
        BeanUtil.copyProperties(warehouseManage,warehouseManage1);
        Assert.isTrue(warehouseManageService.updateById(warehouseManage1), "修改失败");
        return R.success(true,"修改成功");
    }

    /**
    * 通过id删除仓库表
    * @param id id
    * @return Result
    */
    @GetMapping("/deleteById")
    @ApiOperation(value = "通过id删除仓库表", notes = "传入id")
    public R<Boolean> deleteById(
            @ApiParam(value = "id", required = true) @RequestParam("id") Long id) {
        Assert.isTrue(warehouseManageService.removeWarehouseManageById(id), "操作失败");
        return R.success(true,"删除成功");
    }


    /**
     * 查询所有仓库
     * @return Result
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "查询所有仓库")
    public R<List<WarehouseManage>> getAll() {
        return R.success(warehouseManageService.list(),"查询成功");
    }
}